package mao.exaimnation.dji;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main1 {
    private static boolean[] marked;
    static int minCost = Integer.MAX_VALUE;
    static int curCost = 0;
    static int target = 0;
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int n = scanner.nextInt();
            marked = new boolean[n];
            curCost=0;

            int m = scanner.nextInt();
            ArrayList<Map.Entry<Integer, Integer>>[] adj = new ArrayList[n];//注意这里的泛型数组的写法。
            for (int i = 0; i < n; i++) {
                adj[i] = new ArrayList<>();
            }

            scanner.nextLine();  //跳过当前行
            for (int i = 0; i < m; i++) {
                int start = scanner.nextInt();
                int end = scanner.nextInt();
                int worth = scanner.nextInt();
                adj[start].add(new MyPath(end, worth));
                adj[end].add(new MyPath(start, worth));
            }
            target = scanner.nextInt();
            dfs(0,adj);
            System.out.println(minCost);
        }
    }

    public static void dfs(int v,ArrayList<Map.Entry<Integer, Integer>>[] adj){
        if (marked[v]) return;
        marked[v] = true;
        if (v == target){
            minCost = Math.min(minCost,curCost);
            marked[v] = false;
            return;
        }
        for (int i = 0; i < adj[v].size(); i++) {
            Map.Entry<Integer, Integer> entry = adj[v].get(i);
            curCost += entry.getValue();
            dfs(entry.getKey(),adj);
            curCost -= entry.getValue();
        }
        marked[v] = false;

        HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
    }

    //Map.Entry 是个接口。
    static class MyPath implements Map.Entry<Integer,Integer>{
        int key;
        int value;

        public MyPath(int key, int value) {
            this.key = key;
            this.value = value;
        }
        @Override
        public Integer getKey() {
            return null;
        }
        @Override
        public Integer getValue() {
            return value;
        }

        @Override
        public Integer setValue(Integer value) {
            return null;
        }
    }

}
